package com.fndsoft.police.xuancheng.service;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.IBinder;

import com.fndsoft.police.xuancheng.R;
import com.fndsoft.police.xuancheng.componet.colog.COLog;
import com.fndsoft.police.xuancheng.data.EventBusData;
import com.fndsoft.police.xuancheng.domain.ThreadMessage;
import com.fndsoft.police.xuancheng.net.HttpAsyncTask;
import com.fndsoft.police.xuancheng.net.HttpReturnMessage;
import com.fndsoft.police.xuancheng.net.receive.ReceiveService;

/**
 * @author carlos carlosk@163.com
 * @version 创建时间：2012-9-30 上午6:52:38 类说明
 */

public class HttpService extends BaseService {
	/**
	 * 从前端收到的事件
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年1月6日 下午6:58:51
	 * @param threadMessage
	 */
	public void onEvent(final ThreadMessage threadMessage) {
		COLog.d(TAG, "onEvent" + threadMessage);
		if (null != threadMessage) {
			// 处理发送前的数据处理
			new Thread(new Runnable(){

				@Override
				public void run() {
					HttpAsyncTask httpAsyncTask = new HttpAsyncTask();
					httpAsyncTask.execute(threadMessage);
				}}).start();
		}
	}

	public static final String TAG = "HttpService";

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		EventBusData.getSendToHttpEventBus().register(this);
		COLog.d("MyService:onCreate");
	}

	@Override
	public void onDestroy() {
		EventBusData.getSendToHttpEventBus().unregister(this);
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	/**
	 * 接收到从服务器发来的结果
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-11-7 下午2:20:54
	 */
	public static void receiveResult(HttpReturnMessage result) {
		// 成功还是失败
		ThreadMessage threadMessage = result.getThreadMessage();
		threadMessage.setReceviceJsonContent(result.getResultContent());
		if (null != result && result.isSuccess()) {
			COLog.d("receiveResult  成功");
			ReceiveService.receiveData(result);
		} else {
			fillThreadMessageError(threadMessage, result);
		}
		if (null != threadMessage.getReturnUICommonLinstener()) {
			threadMessage.getReturnUICommonLinstener()
					.onClickOne(threadMessage);
		} else {
			EventBusData.getSendToUIEventBus().post(threadMessage);
		} 
	}

	/**
	 * 处理服务端返回的错误信息
	 * 
	 * @author Roy
	 * @version 创建时间:2013-11-28上午11:20:27
	 * @param threadMessage
	 * @param result
	 */
	private static void fillThreadMessageError(ThreadMessage threadMessage,
			HttpReturnMessage result) {
		// 赋值错误编号
		// 连接网络返回的错误相关信息
		Exception e = result.getResultException();
		if (null == e) {
			threadMessage.setErrorCodeResId(R.string.error_unknownerror);
			return;
		}
		int errorCode = 0;
		COLog.d("receiveResult 失败 e="
				+ result.getResultException().getMessage());

		if (e.getClass() == SocketTimeoutException.class) {
			errorCode = R.string.error_servernotexist;
		} else if (e.getClass() == ConnectException.class) {
			errorCode = R.string.error_netwokerror;
		} else if (e.getClass() == SocketException.class) {
			errorCode = R.string.error_netwokerror;
		} else if (e.getClass() == ParserConfigurationException.class) {
			errorCode = R.string.error_xmlerror;
		} else if (e.getClass() == SAXException.class) {
			errorCode = R.string.error_xmlerror;
		} else if (e.getClass() == SQLiteException.class) {
			errorCode = R.string.error_sqliteerror;
		} else {
			errorCode = R.string.error_unknownerror;
		}
		threadMessage.setErrorCodeResId(errorCode);
	}

}
